/*
 * @Description: Index
 * @Author: zoeblow
 * @Email: wangfuyuan@nnuo.com
 * @Date: 2019-11-22 13:05:03
 * @LastEditors  : zoeblow
 * @LastEditTime : 2019-12-30 18:50:17
 * @FilePath: \monitor\index.js
 */
const express = require("express");
const bodyParser = require("body-parser");
const ejs = require("ejs");
const path = require("path");

const app = express();
/**
 * @description: 配置node服务端口
 */
const port = "3000";

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

const Utils = require("./utils");
/**
 * @description: 设置于静态资源目录
 */
app.use(express.static("public"));

/**
 * @description: 设置 View
 */
app.set("views", path.join(__dirname, "/views")); // 添加视图路径
app.engine("html", ejs.__express); // 将EJS模板映射至".html"文件
app.set("view engine", "html"); // 设置视图引擎

//设置跨域访问
app.all("*", (req, res, next) => {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "X-Requested-With");
  res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
  res.header("X-Powered-By", " 3.2.1");
  res.header("Content-Type", "application/json;charset=utf-8");
  next();
});
// routes
const getUser = require("./routes/getUser.js");
const getPV = require("./routes/getPV.js");
const upLog = require("./routes/upLog.js");

// schema
// const { User } = require("./schema/user");

app.use("/user", getUser);
app.use("/pv", getPV);
app.use("/upLog", upLog);

app.get("/", (req, res) => {
  res.type("html");
  res.render("index", { title: "首页" });
});

app.get("/getData", function(req, res) {
  var result = req.query; //这个是前台穿过来的表单数据
  console.log(result, "getData-result");
  const obj = {
    code: 404,
    mesage: "接口报错了"
  };
  res.send(obj); //返回数据
});

app.post("/pageTime", function(req, res) {
  var result = req.body;
  console.log(result, "time-result");
  const obj = {
    code: 404,
    mesage: "接口报错了"
  };
  res.send(obj); //返回数据
});

/**
 * @description: Run
 */
app.listen(port, () => {
  console.log(`Server is started at http://127.0.0.1:${port}`);
});
